package com.dayuan.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.dayuan.bean.BackUser;
import com.dayuan.service.AdminUserService;
import com.dayuan.utils.PatternUtils;

@Controller
@RequestMapping("/register")
public class RegisterController {

	@Resource
	private AdminUserService adminUserService;
	
	//注册页面
	@RequestMapping(value = "/login.shtml", method = { RequestMethod.POST, RequestMethod.GET })
	public String login(ModelMap ms) {
		return "register";
	}
	
	//注册帐号
	@RequestMapping(value = "/register.shtml", method = { RequestMethod.POST, RequestMethod.GET })
	public String register(ModelMap modelMap,BackUser adminUser,String passWordRepeat) throws Exception {
		if (!adminUser.getPassword().equals(passWordRepeat)) {
			modelMap.addAttribute("msg", "两次密码不一致");
			return "redirect:login.shtml";
		}
		if(!PatternUtils.isEmail(adminUser.getEmail())){
			modelMap.addAttribute("msg", "请输入正确邮箱");
			return "redirect:login.shtml";
		}		
		try {			
			adminUserService.insertUser(adminUser);
	
			return "redirect:/login/hello.shtml";
		} catch (Exception e) {
			e.printStackTrace();
			modelMap.addAttribute("msg", "网络错误");
			return "redirect:login.shtml";
		}

	}
	

}